/**
 *<p>Copyright: Copyright (c) 2011</p> 
 *<p>Company: 青果科技</p>
 *@author xiezhouyan
 *@date2011-11-23下午08:27:58 
 *@version 1.0
 *@description:
 */
package com.cybertron.ironhide.manage.common.base;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.cybertron.ironhide.manage.common.Constants;
import com.cybertron.ironhide.manage.util.JsonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5557228859840964878L;

	public static final String VIEW = "view";

	public static final String LIST = "list";

	public static final String ADD = "add";

	public static final String EDIT = "edit";

	public static final String DELETE = "delete";

	public static final String DETAIL = "detail";

	public static final String CLOSE_CURRENT = "closeCurrent";

	private static final String STATESCODE = "statusCode";

	private static final String MESSAGE = "message";

	private static final String[] resultKeys = new String[] { "navTabId",
			"rel", "callbackType", "forwardUrl" };

	protected Long id;

	protected Long[] ids;

	protected Pager pager = new Pager();

	protected File filedata;

	protected String filedataFileName;

	protected String q;

	protected Integer limit;

	// 获取Attribute
	public Object getAttribute(String name) {
		return ServletActionContext.getRequest().getAttribute(name);
	}

	// 设置Attribute
	public void setAttribute(String name, Object value) {
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	// 获取Parameter
	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	// 获取Parameter数组
	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	// 获取Session
	public Object getSession(String name) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session.get(name);
	}

	// 获取Session
	public Map<String, Object> getSession() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session;
	}

	// 设置Session
	public void setSession(String name, Object value) {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.put(name, value);
	}

	// 获取Request
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	// 获取Response
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	// 获取Application
	public ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}

	// AJAX输出，返回null
	public String ajax(String content, String type) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Connection", "keep-alive");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// AJAX输出文本，返回null
	public String ajaxText(String text) {
		return ajax(text, "text/plain");
	}

	// AJAX输出HTML，返回null
	public String ajaxHtml(String html) {
		return ajax(html, "text/html");
	}

	// AJAX输出XML，返回null
	public String ajaxXml(String xml) {
		return ajax(xml, "text/xml");
	}

	// 根据字符串输出JSON，返回null
	public String ajaxJson(String jsonString) {
		return ajax(jsonString, "text/html");
	}

	// 根据Map输出JSON，返回null
	public String ajaxJson(Map<String, Object> jsonMap) {
		return ajaxJson(JsonUtil.writeValueAsString(jsonMap));
	}

	public String ajaxText(Map<String, Object> jsonMap) {
		return ajaxText(JsonUtil.writeValueAsString(jsonMap));
	}

	public String ajaxJsonArray(Collection<?> array) {
		return ajax(JsonUtil.writeValueAsString(array), "text/html");
	}

	protected boolean isAjax() {
		HttpServletRequest request = getRequest();
		String xRequestedWith = request.getHeader("x-requested-with");
		return StringUtils.equals(xRequestedWith, "XMLHttpRequest");
	}

	// 输出JSON,成功消息
	public String ajaxSuccess(String message, String... params) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATESCODE, "200");
		jsonMap.put(MESSAGE, message);
		for (int i = 0; i < params.length; i++) {
			if (i + 1 > resultKeys.length) {
				return ajaxJson(jsonMap);
			}
			if (StringUtils.isBlank(params[i])) {
				continue;
			}
			jsonMap.put(resultKeys[i], params[i]);
		}
		return ajaxJson(jsonMap);
	}

	// 输出JSON,错误消息
	public String ajaxError(String message, String... params) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATESCODE, "300");
		jsonMap.put(MESSAGE, message);
		for (int i = 0; i < params.length; i++) {
			if (i + 1 > resultKeys.length) {
				return ajaxJson(jsonMap);
			}
			if (StringUtils.isBlank(params[i])) {
				continue;
			}
			jsonMap.put(resultKeys[i], params[i]);
		}
		return ajaxJson(jsonMap);
	}

	// 输出JSON,错误消息
	public String ajaxLogout(String message, String... params) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATESCODE, "301");
		jsonMap.put(MESSAGE, message);
		for (int i = 0; i < params.length; i++) {
			if (i + 1 > resultKeys.length) {
				return ajaxJson(jsonMap);
			}
			if (StringUtils.isBlank(params[i])) {
				continue;
			}
			jsonMap.put(resultKeys[i], params[i]);
		}
		return ajaxJson(jsonMap);
	}

	// 设置页面不缓存
	public void setResponseNoCache() {
		getResponse().setHeader("progma", "no-cache");
		getResponse().setHeader("Cache-Control", "no-cache");
		getResponse().setHeader("Cache-Control", "no-store");
		getResponse().setDateHeader("Expires", 0);
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getId() {
		return id;
	}

	public Long[] getIds() {
		return ids;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getResourceRootUrl() {
		return Constants.RESOURCE_ROOT_URL;
	}

	public String getBase() {
		return getRequest().getContextPath();
	}

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String getFiledataFileName() {
		return filedataFileName;
	}

	public void setFiledataFileName(String filedataFileName) {
		this.filedataFileName = filedataFileName;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

}
